package com.example.rbac.controller;

import com.example.rbac.exception.ParamException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.List;

/**
 * @author XieLongzhen
 * @date 2018/12/16 1:08
 */
@Slf4j
public class BaseController {
    /**
     * 生成校验失败响应
     *
     * @param bindingResult
     * @return
     */
    protected void generateValidateFailedResponse(BindingResult bindingResult) throws ParamException {
        List<ObjectError> errorList = bindingResult.getAllErrors();
        StringBuilder builder = new StringBuilder("参数校验失败：");
        for (ObjectError error : errorList) {
            if (error instanceof FieldError) {
                FieldError fieldError = (FieldError) error;
                builder.append(fieldError.getField());
                builder.append(fieldError.getDefaultMessage());
                builder.append("！");
            }
        }
        String msg = builder.toString();
        log.info(msg);
        throw new ParamException(msg);
//        return JsonData.fail(msg);
    }
}
